package com.example.zzl.view.fragment.ShareOrderFragment.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zzl.R;
import com.example.zzl.view.fragment.ShareOrderFragment.ShareInformation;
import com.example.zzl.view.fragment.ShareOrderFragment.disan.JsonAsyncTask;
import com.example.zzl.view.fragment.ShareOrderFragment.disan.ListUtils;
import com.example.zzl.view.fragment.ShareOrderFragment.fragment.MyListView;
import com.example.zzl.view.fragment.ShareOrderFragment.model.Choice;
import com.example.zzl.view.fragment.ShareOrderFragment.model.User;
import com.example.zzl.view.fragment.ShareOrderFragment.model.User_hudong;
import com.example.zzl.view.fragment.ShareOrderFragment.model.Userinfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

;

/**
 * Created by Administrator on 2017/2/8.
 */

public class ShareChoiceAdapter extends BaseAdapter {
    private Context context;
    private List<Choice.InfolistBean> list;
    private List<Choice.InfolistBean.ReviewinfolistBean> olist;
    private PoplvAdapter poplvAdapter;
    private List<User> au;
    private List<User.InfolistBean> aulist;
    private Handler handler;
    private SharePinglistviewAdapter adapter;
    private PopupWindow pw;
    private ListView poplv;
private RelativeLayout rl;
    private String url="http://mws.biyabi.com/WebService.asmx/GetShareShoppingInfoByUserIDs\n" +
            "?ShowFlag=1&Status=1&LogUserID=0&PageSize=10&Page=1&UserID=";

    private String userinfoURL="http://mws.biyabi.com/WebService.asmx/GetUserQueryByUserID?UserID=";
    private String userconectonURL="http://mws.biyabi.com/WebService.asmx/GetOtherCount?userid=0&pwd&beuserid=";

    private List<Userinfo> userinfoList;
    private List<User_hudong> conList;

    public ShareChoiceAdapter(Context context, List<Choice.InfolistBean> list) {
        this.context = context;
        this.list = list;

    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
//    public void chick(View view) {
//
//    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        olist=new ArrayList<>();
        au=new ArrayList<>();
        aulist=new ArrayList<>();

//        userinfoList=new ArrayList<>();
//        conList=new ArrayList<>();

        final ViewHolder vh;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = View.inflate(context, R.layout.suipian, null);


            vh.tv_author_name = (TextView) convertView.findViewById(R.id.share_autor_name);
            vh.tv_createtime = (TextView) convertView.findViewById(R.id.share_autor_createtime);
            vh.tv_show_num = (TextView) convertView.findViewById(R.id.fenmu);
            vh.tv_show_neirong = (TextView) convertView.findViewById(R.id.show_neirong);
            vh.tv_x_tiaopinglun = (TextView) convertView.findViewById(R.id.xtiao);
            vh.tv_xiangguan_jieshao = (TextView) convertView.findViewById(R.id.share_thing_ieshao);
            vh.tv_xiangguan_jiage = (TextView) convertView.findViewById(R.id.share_jiage);
            vh.tv_shoucang = (TextView) convertView.findViewById(R.id.share_dianzan);

            vh.tv_chakan = (TextView) convertView.findViewById(R.id.bu1);

            vh.listView=(MyListView)convertView.findViewById(R.id.pinglv);

            vh.iv_author_img = (ImageView) convertView.findViewById(R.id.share_autor_img);
            vh.iv_show = (ImageView) convertView.findViewById(R.id.show);
            vh.iv_xiangguan = (ImageView) convertView.findViewById(R.id.share_thing_img);

            rl=(RelativeLayout) convertView.findViewById(R.id.rl);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        vh.tv_chakan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vh.tv_chakan.getText().equals("查看全文")){
                    vh.tv_chakan.setText("收起");
                    vh.tv_chakan.setMaxLines(20);
                }else if(vh.tv_chakan.getText().equals("收起")){
                    vh.tv_chakan.setText("查看全文");
                    vh.tv_chakan.setMaxLines(6);
                }
            }
        });


      //  vh.tv_show_neirong.setOnClickListener(this);

        vh.tv_author_name.setText(list.get(position).NickName);
        vh.tv_createtime.setText(list.get(position).UpdateTime);
        int size = list.get(position).ImageTags.size();

            vh.tv_show_num.setText(size+"");

        vh.tv_show_neirong.setText(list.get(position).InfoContent);

        vh.tv_show_neirong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ShareInformation.class);

                intent.putExtra("shangpinid",list.get(position).InfoID);
                intent.putExtra("yemianid",list.get(position).SSID);
                Log.e("adsfasdf",list.get(position).InfoID+"");
                context.startActivity(intent);
            }
        });


          vh.tv_x_tiaopinglun.setText(list.get(position).CommentCount+"");

        vh.tv_xiangguan_jieshao.setText(list.get(position).InfoTitle);
        if ((int)list.get(position).MinPriceRMB==(int)list.get(position).MaxPriceRMB) {
            vh.tv_xiangguan_jiage.setText((int)list.get(position).MaxPriceRMB+"");
        }else if ((int)list.get(position).MinPriceRMB!=(int)list.get(position).MaxPriceRMB){
            vh.tv_xiangguan_jiage.setText((int)list.get(position).MinPriceRMB+"~"+(int)list.get(position).MaxPriceRMB);
        }
        String praiseGoodCount = list.get(position).PraiseGoodCount + "";
//        if (praiseGoodCount.length()>0){
        vh.tv_shoucang.setText(list.get(position).PraiseGoodCount+"");

       // final BitmapPool bitmapPool = Glide.get(context).getBitmapPool();

        Glide.with(context).load(list.get(position).HeadImage).bitmapTransform(new CropCircleTransformation(context)).into(vh.iv_author_img);

        vh.iv_author_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
                int wid=displayMetrics.widthPixels;

                int hei= displayMetrics.heightPixels;
               // Toast.makeText(context,list.get(position).UserID+"",Toast.LENGTH_SHORT).show();
                View view=View.inflate(context,R.layout.pop,null);

                poplv=(ListView)view.findViewById(R.id.poplv);


                loadDa();
                loadDa1();
                View head=View.inflate(context,R.layout.pophead,null);
                final Vholder h=new Vholder();
                h.name=(TextView)head.findViewById(R.id.pophead_name);
                h.author_img=(ImageView) head.findViewById(R.id.pop_img);
                h.fanhui=(ImageView)head.findViewById(R.id.fanhi);
                h.zan=(TextView)head.findViewById(R.id.duoshaozan);
                h.hehe=(TextView)head.findViewById(R.id.zan);

                h.fanhui.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                    }
                });

                handler=new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);


                        if (msg != null) {
                            Log.e("11111","ss");
                            switch (msg.what){
                                case 100:
                                    Bundle bu=new Bundle();
                                    bu=msg.getData();


                                    h.name.setText(bu.getString("name"));

                                    Glide.with(context).load(bu.getString("auimg")).bitmapTransform(new CropCircleTransformation(context)).into(h.author_img);

                                    break;
                                case 111:
                                    Bundle bb=msg.getData();
                                    h.zan.setText("获赞"+bb.getInt("ss"+"个"));
                                    h.hehe.setText(bb.getInt("beizan")+"  晒单    "+bb.getInt("fan")+"  粉丝   "
                                    +bb.getInt("befan")+"  关注");


                                    break;

                        }


                            }

                        //String s = msg.obj.toString();

                    }
                };


//



                poplvAdapter=new PoplvAdapter(context,aulist);
                loadData();
poplv.addHeaderView(head);
                poplv.setAdapter(poplvAdapter);

                    pw=new PopupWindow(view,wid,hei);
                pw.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
                pw.setOutsideTouchable(true);
                pw.setAnimationStyle(R.style.pop);
                    pw.showAtLocation(rl, Gravity.RIGHT,wid,0);

               // }
            }

            private void loadDa1() {
                new JsonAsyncTask(context, new JsonAsyncTask.CallBack() {
                    @Override
                    public void sendData(String json) {
                        Type type=new TypeToken<ArrayList<User_hudong>>(){}.getType();
                        conList=new Gson().fromJson(json,type);
                        Message msg1 = handler.obtainMessage();
                        msg1.what=111;
                        Bundle b1=new Bundle();
                        b1.putInt("beizan",conList.get(0).SSCount);
                        b1.putInt("ss",conList.get(0).GCount);
                        b1.putInt("beifan",conList.get(0).BeFansCount);
                        b1.putInt("fan",conList.get(0).FansCount);
                        msg1.setData(b1);
                        handler.sendMessage(msg1);

                    }
                }).execute(userconectonURL+list.get(position).UserID);

            }

            class Vholder{
                TextView name,zan,hehe;
                ImageView author_img,fanhui;
            }

            private void loadDa() {

                new JsonAsyncTask(context, new JsonAsyncTask.CallBack() {
                    @Override
                    public void sendData(String json) {
                        Type type=new TypeToken<ArrayList<Userinfo>>(){}.getType();
                        userinfoList=new Gson().fromJson(json,type);
                        Message msg = handler.obtainMessage();
                        msg.what=100;
                        Bundle b=new Bundle();
                        b.putString("name",userinfoList.get(0).nickName);
                        b.putString("auimg",userinfoList.get(0).HeadImage);


                        msg.setData(b);
                        handler.sendMessage(msg);

                        //Userinfo info= userinfoList.get(0);

                        Log.e("sendData: ", userinfoList.get(0).nickName);
                        //  aulLoist.addAll(user1.infolist);

                    }
                }).execute(userinfoURL+list.get(position).UserID);


            }


            private void loadData() {
                new JsonAsyncTask(context, new JsonAsyncTask.CallBack() {
                    @Override
                    public void sendData(String json) {
                        Type type=new TypeToken<ArrayList<User>>(){}.getType();
                       // Object o = new Gson().fromJson(json, type);
                        au=new Gson().fromJson(json, type);
                        User user1 = au.get(0);

                        aulist.addAll(user1.infolist);
                        poplvAdapter.notifyDataSetChanged();


                    }
                }).execute(url+list.get(position).UserID);
            }
        });

        Glide.with(context).load(list.get(position).MainImage).into(vh.iv_show);
//        RelativeLayout.LayoutParams p= (RelativeLayout.LayoutParams) vh.iv_show.getLayoutParams();
//        p.height=vh.iv_show.getWidth();
//        vh.iv_show.setLayoutParams(p);
        Glide.with(context).load(list.get(position).MainImage).into(vh.iv_xiangguan);

        olist.addAll(list.get(position).reviewinfolist);


adapter=new SharePinglistviewAdapter(context,olist);
        vh.listView.setAdapter(adapter);
        ListUtils.setListViewHeight(vh.listView);
        return convertView;
    }



    class ViewHolder {
        TextView tv_author_name, tv_createtime, tv_show_num, tv_show_neirong, tv_x_tiaopinglun,
                tv_xiangguan_jieshao, tv_xiangguan_jiage, tv_shoucang, tv_chakan;
        ImageView iv_author_img, iv_show, iv_xiangguan;
        MyListView listView;
    }
}



